# -*- encoding:utf-8 -*-
import urllib
import simplejson
import re


def getSongInfobyId(id):
    return simplejson.dump(dic)

def parserSongHtml(id):
    return None

#cdinfo
"""
<table id="cd_information" cellpadding="0" cellspacing="0">
***
</table>
"""

#artistinfo
#<a href="/artist/7207" title="">谭维维</a>

#albuminfo
#<a href="/album/442827" title="">害怕爱上你</a>

#language
"""<tr>
              <td class="dcl">语种</td>
                            <td>国语</td>
                                        </tr>
                                        """

#唱片公司
#<a href="/search?key=远达卓越" title="">远达卓越</a>

#发行时间

#各级评分

#相关图片
#<img class="cdCDcover185" src="http://img.xiami.com/./images/album/img60/1260/33354_2.jpg">
def getCoverPicLink(songid):
    s = getHtmlSource(songid)
    imgregex = ur"cdCDcover185(\s|\S)*?>"
    imgsearch = re.search(imgregex,s)
    if imgsearch:
        imgstr = s[imgsearch.start():imgsearch.end()]
    else:
        f = open("songpagetmp.txt","w")
        print>>f,s
        f.close()
        raise Exception("song page format error")
    #print imgstr
    link = imgstr[imgstr.index("src")+5:imgstr.rindex("\"")]
    return link

def getHtmlSource(songid):
    theurl = "http://www.xiami.com/song/"+str(songid)
    l = urllib.urlopen(theurl)
    s = l.read()
    l.close()
    return s

if __name__ == "__main__":
    print getCoverPicLink("1770224315")
